<?php
// +----------------------------------------------------------------------
// | Copyright © Sogu Technologies Co. Ltd 2018-2050 All rights reserved.
// +----------------------------------------------------------------------
// | File name: AuthUserCert.php
// +----------------------------------------------------------------------
// | Author: liuju
// +----------------------------------------------------------------------
// | Description: 描述文档功能
// +----------------------------------------------------------------------
// | Others: 其他内容的说明
// +----------------------------------------------------------------------
// | History: 修改历史记录列表，每条修改记录应包含修改日期、修改者及修改内容简述
// +----------------------------------------------------------------------
// | 1.Date: 2019-03-11 Author: liuju Modification: 创建
// +----------------------------------------------------------------------

namespace App\ModelTransfers\WsUsers;


use App\Models\UserCertOcr;
use App\ModelTransfers\Base;
use Carbon\Carbon;
use function Faker\Provider\pt_BR\check_digit;
use Illuminate\Support\Facades\DB;

class AuthUserCert extends Base
{

    protected $table = 'ws_user.auth_user_certification';


    public static function transfer($user,$userId,$certkskdUserId)
    {
        if(in_array($user,$certkskdUserId)){
            return ;
        }
       $data = DB::connection('transfer')
           ->table('ws_user.auth_user_certification as cert')
           ->select([
               DB::raw('c.oss_url as id_card_front'),
               DB::raw('d.oss_url as id_card_back'),
               DB::raw('e.oss_url as hand_cert'),
               DB::raw('cert.name'),
               DB::raw('cert.num'),
               DB::raw('cert.address as domicile_address'),
               DB::raw('cert.issue as issuing_authority'),
               DB::raw('cert.start_date as valid_date_begin'),
               DB::raw('cert.end_date as valid_date_end'),
               DB::raw('cert.create_date as created_at'),
               DB::raw('cert.update_date as updated_at')
           ])
           ->leftJoin('ws_common.common_attachment as c','c.business_no','=', 'cert.identify_card_front_file_id')
           ->leftJoin('ws_common.common_attachment as d','d.business_no','=', 'cert.identify_card_back_file_id')
           ->leftJoin('ws_common.common_attachment as e','e.business_no','=', 'cert.identify_card_hold_file_id')
           ->where('cert.user_id',$user->id)
           ->where('cert.is_valid',1)
           ->first();
       if($data){
           $data = (array) $data;
           $data['is_valid'] = 1;
           $keyDate = ['valid_date_begin','valid_date_end'];
           foreach($data as $key=>$value) {
               if(!$value){
                   $data[$key] = '';
               }
               if(in_array($key,$keyDate) && $data[$key]){
                   $len = mb_strlen($value);
                  if( $len <6){
                       $data[$key] = '';
                       $data['is_valid'] = 0;
                   }else if ($len == 6){
                      $data[$key] .='01';
                  }else{
                      $data[$key] = strtotime($data[$key]) ? Carbon::parse($data[$key]) :'长期';
                  }
               }
           }
           $data['old_kskd_user_id'] = $user->id;
           $data['user_id'] = $userId;
           UserCertOcr::create($data);
       }


    }


    /**
     * Description:删除迁移的身份证
     * Author: liuju
     * DateTime: 2019-03-12 16:43
     * @throws \Exception
     */
    public  static  function removeTransfer($userId)
    {
       return  UserCertOcr::whereNotNull('old_kskd_user_id')
           ->when($userId,function($query) use($userId){
               return $query->where('user_id',$userId);
           })
           ->delete();
    }
}